//
//  UIBarButtonItem+Extension.swift
//  DouYuZB
//
//  Created by James on 2017/10/13.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    //类方法
    
    class func createNavigationBarItem(normalImg:String,highlightImg:String,size:CGSize = CGSize(width: 40, height: 40)) -> UIBarButtonItem {
        
        let btn = UIButton()
        btn.setImage(UIImage(named:normalImg), for: .normal)
        btn.setImage(UIImage(named:highlightImg), for: .highlighted)
        btn.frame = CGRect(origin: CGPoint(), size: size)
        
        return UIBarButtonItem(customView: btn)
    }
    
    //构造函数  只能扩展便利构造函数
    //条件：1> convenience 开头  2>在构造函数中必须明确的调用一个设计的构造函数 (self)
    convenience init(normalImg:String,highlightImg:String = "",size:CGSize = CGSize()) {
        let btn = UIButton()
        
        btn.setImage(UIImage(named:normalImg), for: .normal)
        
        if highlightImg != ""{
             btn.setImage(UIImage(named:highlightImg), for: .highlighted)
        }
        if size == CGSize(){
           btn.sizeToFit()
        }else{
          btn.frame = CGRect(origin: CGPoint(), size: size)
        }

        self.init(customView: btn)
    }
    
}
